<?php
	//Ativa o Buffer que armazena o conteúdo principal da página
	ob_start();
	require_once("../autoload.php");
	$sessao = new Projeto_Sessao();
	$sessao->setSessionName("admin");
	$sessao->iniciaSessao();
	
	if(isset($_POST['btSalvar'])){
		if($_POST['hdCodUser']){
			include("usuarios/atualizar.php");
		}else{
			include("usuarios/inserir.php");
		}
	}
	
	if(isset($_POST['btExcluir'])){
		include("usuarios/excluir.php");
	}
	
	$codAdmin = $sessao->getVarSessao('codAdmin');
	$usuario = new Projeto_Usuario();
	$usuario->setCodigo($codAdmin);
	$usuario->Carrega();
?>
<form method="post">
<input type="hidden" value="<?php echo $usuario->getCodigo();?>" name="hdCodUser" id="hdCodUser" />
<fieldset>
	<legend>Usuário</legend>
	<table width="100%">
		<tr>
			<td width="10%" align="left">Nome: </td>
			<td align="left" colspan="3"><input name="txtNome" id="txtNome" type="text" size="60" value="<?php echo $usuario->getNome();?>" /> </td>
		</tr>
		<tr>
			<td align="left">Login: </td>
			<td align="left" colspan="3"><input name="txtLogin" id="txtLogin" type="text" size="20" value="<?php echo $usuario->getLogin();?>" /> </td>
		</tr>
		<tr>
			<td align="left">Senha: </td>
			<td align="left" colspan="3"><input name="txtSenha" id="txtSenha" type="password" size="20" /> </td>
		</tr>
		<tr>
			<td align="left">Confirmação: </td>
			<td align="left" colspan="3"><input name="txtConfirmacao" id="txtConfirmacao" type="password" size="20" /> </td>
		</tr>
		<tr>
			<td align="left">Nivel: </td>
			<td width="16%" align="left">
				<select name="cmbNivel" id="cmbNivel">
					<option value="B" <?php if($usuario->getNivel() == "B"){ echo "selected=\"selected\"";}?>>Baixo</option>
					<option value="M" <?php if($usuario->getNivel() == "M"){ echo "selected=\"selected\"";}?>>Médio</option>
					<option value="A" <?php if($usuario->getNivel() == "A"){ echo "selected=\"selected\"";}?>>Alto</option>
				</select>
			</td>
			<td width="11%" align="left">Ultimo login: </td>
			<td width="63%" align="left"><input name="txtUltLogin" id="txtUltLogin" type="text" size="20" readonly="readonly" value="<?php echo Util_Util::DateMysqlToDatePt($usuario->getUltimoLogin());?>" /></td>
		</tr>
		<tr>
			<td align="left" colspan="4">
				<input type="button" value="Novo" name="btNovo" class="botao" onclick="limpaCampos('txtNome|txtLogin|txtSenha|txtConfirmacao|hdCodUser|txtUltLogin')" />
				<input type="submit" value="Salvar" name="btSalvar" class="botao" />
				<input type="submit" value="Excluir" name="btExcluir" class="botao" />
			</td>
		</tr>
	</table>
</fieldset>
<fieldset>
<div id="listaUsuarios">
<?php
	try{
		$listaDeUsers = $usuario->Lista();
	?>
	<table width="100%" cellpadding="2" cellspacing="0">
		<tr bgcolor="#CCCCCC">
			<th align="center">Cód.</th>
			<th align="center">Nome</th>
			<th align="center">Login</th>
			<th align="center">Nivel</th>
			<th align="center">Ult. Login</th>
			<th align="center">Ações</th>
		</tr>
		<?php
		foreach($listaDeUsers as $key){
			$usuario->setCodigo($key->codigo);
			$usuario->Carrega();
		?>
		<tr>
			<td align="center"><?php echo $usuario->getCodigo();?></td>
			<td align="left"><?php echo $usuario->getNome();?></td>
			<td align="center"><?php echo $usuario->getLogin();?></td>
			<td align="center"><?php echo $usuario->getNivel();?></td>
			<td align="center"><?php echo $usuario->getUltimoLogin();?></td>
			<td align="center" id="tdRetorno"> <input type="button" class="botao" value="Editar" onclick="editarUsuario('<?php echo $usuario->getCodigo();?>')" /> </td>
		</tr>
		<?php
		}
		?>
	</table>
	<?php
	}catch(Exception $e){
		Util_Util::Mensagem($e->getMessage());
	}
?>
</div>
</fieldset>
</form>
<?php
	// conteudoDaPagina recebe o conteudo do buffer
	$conteudoDaPagina = ob_get_contents(); 
	
	// Descarta o conteudo do Buffer
	ob_end_clean(); 
	
	/* Atribuição das Variáveis da página principal*/
	$titulo = "Macrofone"; 
	
	//Include com o Template
	include("../masterpage/masterpage_admin.php");
?>